<?php

namespace app\api\command;

use app\common\library\Email;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Log;

class OrderTimeEnd extends Command
{

    protected function configure()
    {
        $this->setName('order_end')->setDescription('【可用时长】到期。订单过期');
    }

    protected function execute(Input $input, Output $output)
    {
        ini_set('default_socket_timeout', -1);  //不超时
        $redis = new \Redis();
        $redis->pconnect(config('redis.host'), config('redis.port'), config('redis.timeout'));
        $redis->select(4);

        // 订单【可用时长】到期
        $redis->psubscribe(['__keyevent@4__:expired'], function ($redis, $pattern, $channel, $message) use ($output) {
            // 订单过期
            Db::startTrans();
            try {
                Db::name('order')->where(['numbers' => $message])->update(['status' => 3, 'updatetime' => time()]);
                Db::commit();
                Log::info('ORDER_TIME_END 订单号：' . $message);
            } catch (\Exception $e) {
                Db::rollback();
                // 记录错误日志
                Log::error('订单【可用时长】到期');
                Log::info('ORDER_TIME_END 订单号：' . $message);
                Log::error($e->getMessage());
            }
        });
    }
}

